package com.ruibang.glass.portal.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.common.domain.DictInfo;
import com.ruibang.glass.portal.components.SsoToken;
import com.ruibang.glass.portal.constant.Constants;
import com.ruibang.glass.portal.constant.UserErrorCode;
import com.ruibang.glass.portal.domain.*;
import com.ruibang.glass.portal.entity.DeptEntity;
import com.ruibang.glass.portal.entity.UserEntity;
import com.ruibang.glass.portal.entity.UserRoleEntity;
import com.ruibang.glass.portal.mapper.UserMapper;
import com.ruibang.glass.portal.mapper.UserRoleMapper;
import com.ruibang.glass.portal.util.*;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

@Service
@Slf4j
public class UserService extends ServiceImpl<UserMapper, UserEntity> implements IService<UserEntity> {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RedisSingleService redisService;

//    @Autowired
//    private RedisSingleService redisService;

    @Autowired
    private SsoToken ssoToken;

    private static final String NINE_STR = "9";


    /**
     * 人脸注册
     */
    private static final String FaceAddURL = "http://47.111.238.73:10002/api/face/interface/faceAdd";
    /**
     * 人脸搜索
     */
    private static final String FaceSearchURL = "http://47.111.238.73:10002/api/face/interface/faceSearch";
    /**
     * 人脸删除
     */
    private static final String FaceDeleteURL = "http://47.111.238.73:10002/api/face/interface/faceDelete";

    /**
     * 查询用户列表
     *
     * @param pageRequest
     * @return
     */
    public ResultBody<PageResult<List<UserEntity>>> getUserList(PageRequest<UserPageParams> pageRequest) {
        UserPageParams param = pageRequest.getParam();
        if (!Optional.ofNullable(param).isPresent()) {
            return ResultBody.fail(UserErrorCode.PARAM_ERROR.getCode(), UserErrorCode.PARAM_ERROR.getMsg());
        }
        try {
            List<Tree<DeptEntity>> deptTrees = deptService.initDeptTree();
            if (StringUtils.isNotBlank(param.getDeptId())) {
                List<Tree<DeptEntity>> childTrees = BuildTree.buildList(deptTrees, param.getDeptId());
                List<String> deptIds = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(childTrees)) {
                    childTrees.forEach(c -> deptIds.add(c.getId()));
                }
                deptIds.add(param.getDeptId());
                param.setDeptId(null);
                param.setDeptIds(deptIds);
            }

            final IPage<UserEntity> page = new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize());
            List<UserEntity> userEntities = baseMapper.getUserList(page, param);

            if (CollectionUtils.isNotEmpty(userEntities)) {
                List<String> userIds = userEntities.stream().map(u -> u.getUserId()).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(userIds)) {

                    //获取换牌角色
                    List<UserRole> userRoles = userRoleMapper.getRoleByUserIds(userIds);
                    if (CollectionUtils.isNotEmpty(userRoles)) {
                        Map<String, List<UserRole>> userRoleMap = userRoles.stream().collect(Collectors.groupingBy(UserRole::getUserId));
                        if (userRoleMap != null && (!userRoleMap.isEmpty())) {
                            for (UserEntity userEntity : userEntities) {
                                List<UserRole> userRoleList = userRoleMap.get(userEntity.getUserId());
                                if (CollectionUtils.isNotEmpty(userRoleList)) {
                                    userEntity.setRoles(userRoleList);
                                }

                                //设置deptName
                                if (StringUtils.isNotBlank(userEntity.getDeptId())) {
                                    StringBuilder deptName = new StringBuilder();
                                    Tree<DeptEntity> deptTree = BuildTree.buildParentTree(deptTrees, userEntity.getDeptId());
                                    getDeptName(deptTree, userEntity.getDeptId(), deptName);
                                    String deptNameStr = deptName.toString();
                                    if (StringUtils.isNotBlank(deptNameStr)) {
                                        userEntity.setDeptName(deptNameStr.substring(deptNameStr.indexOf("/") + 1, deptNameStr.lastIndexOf("/")));
                                    }
                                }

                            }
                        }
                    }
                }
            }

            page.setRecords(userEntities);

            PageResult<List<UserEntity>> pageResult = new PageResult<>(page);

            return ResultBody.success(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return ResultBody.fail(UserErrorCode.USER_LIST_IS_ERROR.getCode(), UserErrorCode.USER_LIST_IS_ERROR.getMsg());
        }
    }


    /**
     * 根据参数获取用户列表
     *
     * @param page
     * @return
     */
    public ResultBody<List<UserEntity>> getUserByParams(UserPageParams page) {
        if (!Optional.ofNullable(page).isPresent()) {
            return ResultBody.fail(UserErrorCode.PARAM_ERROR.getCode(), UserErrorCode.PARAM_ERROR.getMsg());
        }
        return ResultBody.success(baseMapper.getUserList(null, page));
    }

    /**
     * 删除通过用户ID
     *
     * @param userId
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultBody<Void> deleteByUserId(String userId) {
        //获取用户信息
        UserEntity entity = baseMapper.getUserById(userId);
        if (!Optional.ofNullable(entity).isPresent()) {
            return ResultBody.fail(UserErrorCode.USER_IS_NOT_EXISTS.getCode(), UserErrorCode.USER_IS_NOT_EXISTS.getMsg());
        }

        //删除用户
        baseMapper.deleteByUserId(userId);
        //删除用户角色
        userRoleMapper.delByUserId(userId);
        //删除缓存
        if (redisService.hasKey(Constants.REDIS_KEY_USERID + userId)) {
            redisService.del(redisService.get(Constants.REDIS_KEY_USERID + userId).toString(),
                    Constants.REDIS_KEY_USERID + userId);
        }
        redisService.hdel(RedisKey.USER_GROUP_TYPE, userId);

        Serializable userStr = redisService.get(Constants.CHECK_LOGIN_PARAM_INFO);
        if (userStr != null && StringUtils.isNotBlank(userStr.toString())) {
            List<String> userIds = JSON.parseArray(userStr.toString(), String.class);
            userIds.remove(userId);
            redisService.set(Constants.CHECK_LOGIN_PARAM_INFO, JSON.toJSONString(userIds));
        }

        return ResultBody.success();
    }

    /**
     * 保存用户与角色
     *
     * @param req
     */
    public ResultBody<Void> saveUser(SaveUserReq req) {
        UserEntity entity = new UserEntity();
        BeanUtils.copyProperties(req, entity);
        if (!Optional.ofNullable(entity).isPresent()) {
            return ResultBody.fail(UserErrorCode.PARAM_ERROR.getCode(), UserErrorCode.PARAM_ERROR.getMsg());
        }
        String deptId = entity.getDeptId();
        if (StringUtils.isBlank(deptId)) {
            return ResultBody.fail(UserErrorCode.DEPT_ID_IS_NULL.getCode(), UserErrorCode.DEPT_ID_IS_NULL.getMsg());
        }
        String userName = entity.getName();
        if (StringUtils.isBlank(userName)) {
            return ResultBody.fail(UserErrorCode.USERNAME_IS_NULL.getCode(), UserErrorCode.USERNAME_IS_NULL.getMsg());
        }
        /*List<String> roleId = entity.getRoleIds();
        if (!Optional.ofNullable(roleId).isPresent()) {
            return ResultBody.fail(UserErrorCode.ROLE_ID_IS_NULL.getCode(), UserErrorCode.ROLE_ID_IS_NULL.getMsg());
        }*/
        String loginName = entity.getLoginName();
        if (StringUtils.isBlank(loginName)) {
            return ResultBody.fail(UserErrorCode.LOGIN_NAME_IS_NULL.getCode(), UserErrorCode.LOGIN_NAME_IS_NULL.getMsg());
        }
        entity.setLoginName(loginName.replaceAll("\\s*", ""));
        //登录是否已存在
        UserEntity user = baseMapper.getUserByLoginName(loginName);
        if (Optional.ofNullable(user).isPresent()) {
            return ResultBody.fail(UserErrorCode.LOGIN_NAME_IS_EXISTS.getCode(), UserErrorCode.LOGIN_NAME_IS_EXISTS.getMsg());
        }

        entity.setStatus(Constants.UserStatusEnum.NORMAL.getVal());
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        //保存用户
        String salt = Encrypt.getSalt();
        if (StringUtils.isBlank(entity.getPassword())) {
            entity.setPassword("123456");//默认密码
        } else {
            try {
                entity.setPassword(RSAEncrypt.decrypt(entity.getPassword(), Constants.PASSWORD_PRIVATE_KEY));
            } catch (Exception e) {
                return ResultBody.fail(UserErrorCode.PASSWORD_PARSE_ERROR.getCode(), UserErrorCode.PASSWORD_PARSE_ERROR.getMsg());
            }

        }

        entity.setPassword(Encrypt.encryPwd(salt, entity.getPassword()));
        entity.setSalt(salt);
        this.save(entity);

        if (StringUtils.isNotBlank(entity.getImageBase64())) {
            //人脸注册
            try {
                Map<String, String> map = new HashMap<>();
                map.put("imageBase64", entity.getImageBase64());
                map.put("userId", entity.getUserId());
                String result = HttpUtils.doPostJson(FaceAddURL, JSON.toJSONString(map));
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (StringUtils.isBlank(result) || Constants.ZERO_INT != jsonObject.getInteger("code")) {

                    //人脸注册失败删除基本信息
                    baseMapper.deleteByUserId(entity.getUserId());
                    userRoleMapper.delByUserId(entity.getUserId());
                    if (redisService.hasKey(Constants.REDIS_KEY_USERID + entity.getUserId())) {
                        redisService.del(redisService.get(Constants.REDIS_KEY_USERID + entity.getUserId()).toString(),
                                Constants.REDIS_KEY_USERID + entity.getUserId());
                    }
                    return ResultBody.fail(jsonObject.getInteger("code"), jsonObject.getString("message"));
                }
                FaceAddResult faceAddResult = JSON.parseObject(jsonObject.getString("data"), FaceAddResult.class);
                entity.setFaceToken(faceAddResult.getFace_token());
                entity.setLogId(faceAddResult.getLog_id());
                //保存用户face_token人脸图片的唯一标识
                baseMapper.updateById(entity);
            } catch (Exception e) {
                //人脸注册失败删除基本信息
                baseMapper.deleteByUserId(entity.getUserId());
                userRoleMapper.delByUserId(entity.getUserId());
                if (redisService.hasKey(Constants.REDIS_KEY_USERID + entity.getUserId())) {
                    redisService.del(redisService.get(Constants.REDIS_KEY_USERID + entity.getUserId()).toString(),
                            Constants.REDIS_KEY_USERID + entity.getUserId());
                }
                e.printStackTrace();
                return ResultBody.fail(UserErrorCode.ERROR.getCode(), UserErrorCode.ERROR.getMsg());
            }
        }
        //保存用户与角色关系
        Optional.ofNullable(entity.getRoleIds()).ifPresent(a -> {
            a.forEach(r -> {
                UserRoleEntity userRole = new UserRoleEntity();
                userRole.setRoleId(r);
                userRole.setUserId(entity.getUserId());
                userRoleMapper.saveUserRole(userRole);
            });

        });


        Serializable userStr = redisService.get(Constants.CHECK_LOGIN_PARAM_INFO);
        if (userStr != null && StringUtils.isNotBlank(userStr.toString())) {
            List<String> userIds = JSON.parseArray(userStr.toString(), String.class);
            userIds.add(entity.getUserId());
            redisService.set(Constants.CHECK_LOGIN_PARAM_INFO, JSON.toJSONString(userIds));
        }

        //放入缓存
        redisService.hset(RedisKey.USER_GROUP_TYPE, entity.getUserId(), entity.getName());

        return ResultBody.success();
    }


    /**
     * 注册
     *
     * @param params
     * @return
     */
    public ResultBody<Void> register(RegisterParams params) {
        if (!Optional.ofNullable(params).isPresent()) {
            return ResultBody.fail(UserErrorCode.PARAM_ERROR.getCode(), UserErrorCode.PARAM_ERROR.getMsg());
        }
        String userName = params.getUsername();
        if (StringUtils.isBlank(userName)) {
            return ResultBody.fail(UserErrorCode.USERNAME_IS_NULL.getCode(), UserErrorCode.USERNAME_IS_NULL.getMsg());
        }
        String password = params.getPassword();
        if (StringUtils.isBlank(password)) {
            return ResultBody.fail(UserErrorCode.PASSWORD_IS_NULL.getCode(), UserErrorCode.PASSWORD_IS_NULL.getMsg());
        }
        String verifyCode = params.getVerifyCode();
        if (StringUtils.isBlank(verifyCode)) {
            return ResultBody.fail(UserErrorCode.GRAPHIC_IS_NULL.getCode(), UserErrorCode.GRAPHIC_IS_NULL.getMsg());
        }
        //图形验证码是否存在
        if (!redisService.hasKey(Constants.GRAPH_CODE_KEY.concat(verifyCode))) {
            return ResultBody.fail(UserErrorCode.GRAPHIC_IS_ERROR.getCode(), UserErrorCode.GRAPHIC_IS_ERROR.getMsg());
        }
        //用户名是否存在
        UserEntity userEntity = baseMapper.getUserByLoginName(userName);
        if (Optional.ofNullable(userEntity).isPresent()) {
            return ResultBody.fail(UserErrorCode.USER_ID_IS_EXISTS.getCode(), UserErrorCode.USER_ID_IS_EXISTS.getMsg());
        }
        SaveUserReq user = new SaveUserReq();
        user.setDeptId(Constants.DEFAULT_DEPT_ID);
        user.setMobile("");
        user.setLoginName(userName);
        user.setName(userName);
        user.setPassword(password);
        user.setStatus(Constants.UserStatusEnum.NORMAL.getVal());
        return saveUser(user);
    }


    /**
     * 重置密码
     *
     * @param userPass
     * @return
     */
    public ResultBody<Void> updateUserPassword(UserPass userPass) {
        if (!cherUserExist(userPass.getUserId())) {
            return ResultBody.fail(UserErrorCode.USER_IS_NOT_EXISTS.getCode(), UserErrorCode.USER_IS_NOT_EXISTS.getMsg());
        }
        String salt = Encrypt.getSalt();
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(userPass.getUserId());
        userEntity.setSalt(salt);
        try {
            userPass.setPassword(RSAEncrypt.decrypt(userPass.getPassword(), Constants.PASSWORD_PRIVATE_KEY));
        } catch (Exception e) {
            return ResultBody.fail(UserErrorCode.PASSWORD_PARSE_ERROR.getCode(), UserErrorCode.PASSWORD_PARSE_ERROR.getMsg());
        }
        userEntity.setPassword(Encrypt.encryPwd(salt, userPass.getPassword()));
        this.updateById(userEntity);
        return ResultBody.success();
    }

    public boolean cherUserExist(String userId) {
        return Optional.ofNullable(baseMapper.getUserById(userId)).isPresent();
    }


    /**
     * 重置密码
     *
     * @param userPass
     * @return
     */
    public ResultBody<Void> modifyPassword(UserPass userPass) {
        if (userPass == null) {
            return ResultBody.fail(UserErrorCode.PARAM_IS_EMPTY.getCode(), UserErrorCode.PARAM_IS_EMPTY.getMsg());
        }

        UserEntity user = baseMapper.getUserById(userPass.getUserId());
        if (user == null || StringUtils.isBlank(user.getUserId())) {
            return ResultBody.fail(UserErrorCode.USER_IS_NOT_EXISTS.getCode(), UserErrorCode.USER_IS_NOT_EXISTS.getMsg());
        }

        if (StringUtils.isBlank(userPass.getOldPassword())) {
            return ResultBody.fail(UserErrorCode.OLDPASSWORD_IS_NULL.getCode(), UserErrorCode.OLDPASSWORD_IS_NULL.getMsg());
        }
        if (StringUtils.isBlank(userPass.getPassword())) {
            return ResultBody.fail(UserErrorCode.PASSWORD_IS_NULL.getCode(), UserErrorCode.PASSWORD_IS_NULL.getMsg());
        }

        try {
            userPass.setPassword(RSAEncrypt.decrypt(userPass.getPassword(), Constants.PASSWORD_PRIVATE_KEY));
            userPass.setOldPassword(RSAEncrypt.decrypt(userPass.getOldPassword(), Constants.PASSWORD_PRIVATE_KEY));
        } catch (Exception e) {
            return ResultBody.fail(UserErrorCode.PASSWORD_PARSE_ERROR.getCode(), UserErrorCode.PASSWORD_PARSE_ERROR.getMsg());
        }

        if (!user.getPassword().equals(userPass.getOldPassword())) {
            return ResultBody.fail(UserErrorCode.OLDPASSWORD_IS_ERROR.getCode(), UserErrorCode.OLDPASSWORD_IS_ERROR.getMsg());
        }

        String salt = Encrypt.getSalt();
        user.setSalt(salt);
        user.setPassword(Encrypt.encryPwd(salt, userPass.getPassword()));
        this.updateById(user);
        return ResultBody.success();
    }

    /**
     * 更新用户信息
     *
     * @param entity
     */
    public ResultBody<Void> updateUserInfoByUserId(UserEntity entity) {

        if (!cherUserExist(entity.getUserId())) {
            return ResultBody.fail(UserErrorCode.USER_IS_NOT_EXISTS.getCode(), UserErrorCode.USER_IS_NOT_EXISTS.getMsg());
        }
        userRoleMapper.delByUserId(entity.getUserId());
        entity.setUpdateTime(LocalDateTime.now());

        this.updateById(entity);
        Optional.ofNullable(entity.getRoleIds()).ifPresent(userInfoRole -> {
            for (String roleId : userInfoRole) {
                UserRoleEntity entityRole = new UserRoleEntity();
                entityRole.setRoleId(roleId);
                entityRole.setUserId(entity.getUserId());
                userRoleMapper.saveUserRole(entityRole);
            }
        });

        //当用户状态设置为禁用时
        if (entity.getStatus() == 2) {
            ssoToken.deleteToken(Constants.T_SSO_TOKEN_USER.concat(entity.getUserId()));
        }

        //修改缓存
        redisService.hset(RedisKey.USER_GROUP_TYPE, entity.getUserId(), entity.getName());

        return ResultBody.success();
    }

    /**
     * 根据用户id获取用信息
     *
     * @param userId
     * @return
     */
    public ResultBody<UserEntity> getUser(String userId) {
        if (StringUtils.isBlank(userId)) {
            return ResultBody.fail(UserErrorCode.PARAM_ERROR.getCode(), UserErrorCode.PARAM_ERROR.getMsg());
        }
        UserEntity entity = baseMapper.getUserById(userId);
        if (!Optional.ofNullable(entity).isPresent()) {
            return ResultBody.fail(UserErrorCode.USER_IS_NOT_EXISTS.getCode(), UserErrorCode.USER_IS_NOT_EXISTS.getMsg());
        }
        entity.setRoles(userRoleMapper.getRoleByUserId(userId));
        return ResultBody.success(entity);
    }

    /**
     * 根据token获取信息
     *
     * @param token
     * @return
     */
    public ResultBody<BindResponse> getTokenInfo(String token) {
        if (StringUtils.isBlank(token)) {
            return ResultBody.fail(UserErrorCode.PARAM_ERROR.getCode(), UserErrorCode.PARAM_ERROR.getMsg());
        }
        log.info("根据token获取信息 token：" + token);
        String tokenInfo = ssoToken.getByToken(token);
        if (StringUtils.isBlank(tokenInfo)) {
            return ResultBody.fail(UserErrorCode.TOKEN_IS_NOT_EXITST.getCode(), UserErrorCode.TOKEN_IS_NOT_EXITST.getMsg());
        }
        BindResponse response = JSON.parseObject(tokenInfo, BindResponse.class);
        log.info("根据token获取信息 response:" + JSON.toJSONString(response));
        return ResultBody.success(response);
    }

    /**
     * 生成图形验证码
     *
     * @param response
     */
    public void generGraphCode(HttpServletResponse response) {
        String randCode = VerifyCodeUtil.randCode(response);
        log.info("形验证码" + randCode);
        if (redisService == null) {
            System.out.println("redis连接获取不成功......");
        }
        redisService.set(Constants.GRAPH_CODE_KEY.concat(randCode), randCode, Constants.GRAPH_CODE_EXPIRE);
    }

    /**
     * 人脸登录
     *
     * @param params
     * @return
     */
    public ResultBody<BindResponse> faceLogin(LoginParams params, HttpServletRequest request) {
        if (!Optional.ofNullable(params).isPresent()) {
            return ResultBody.fail(UserErrorCode.PARAM_ERROR.getCode(), UserErrorCode.PARAM_ERROR.getMsg());
        }

        //此处进来的只有App(设置默认值)
        params.setUserType(Constants.TWO_STR);

        Map<String, String> map = new HashMap<>();
        map.put("imageBase64", params.getImageBase64());
        String result = HttpUtils.doPostJson(FaceSearchURL, JSON.toJSONString(map));
        JSONObject jsonObject = JSONObject.parseObject(result);
        log.info("百度人脸搜索返回信息：" + jsonObject);
        if (!Constants.ZERO_INT.equals(jsonObject.getInteger("code"))) {
//            return ResultBody.fail(jsonObject.getInteger("code"), jsonObject.getString("message"));
            return ResultBody.fail(UserErrorCode.FACE_SEARCH_ERROR.getCode(), UserErrorCode.FACE_SEARCH_ERROR.getMsg());
        }
        FaceSearchResult faceSearchResult = JSON.parseObject(jsonObject.getString("data"), FaceSearchResult.class);
        List<FaceSearchUserList> lists = faceSearchResult.getUser_list();

        //判断比对值
        if (lists.get(0).getScore() <= Constants.FACE_MATCH_SCORE) {
            return ResultBody.fail(UserErrorCode.FACE_SEARCH_ERROR.getCode(), UserErrorCode.FACE_SEARCH_ERROR.getMsg());
        }

        //根据用户名查询用户是否存在
        UserEntity user = baseMapper.getUserByIdAndFaceToken(lists.get(0).getUser_id());

        if (!Optional.ofNullable(user).isPresent()) {
            return ResultBody.fail(UserErrorCode.USER_IS_NOT_EXISTS.getCode(), UserErrorCode.USER_IS_NOT_EXISTS.getMsg());
        }
        //是否被禁用
        if (!user.normal()) {
            return ResultBody.fail(UserErrorCode.USER_DISABLE.getCode(), UserErrorCode.USER_DISABLE.getMsg());
        }


        BindResponse response = new BindResponse();


        if (StringUtils.isBlank(response.getDeptId())) response.setDeptId(user.getDeptId());
        DeptEntity deptEntity = deptService.getDept(user.getDeptId());
        if (deptEntity != null) {
            response.setDeptName(deptEntity.getDeptName());
            response.setDeptType(deptEntity.getDeptType());
        }

        //换牌角色
        List<UserRole> userRoles = userRoleMapper.getRoleByUserIds(Collections.singletonList(user.getUserId()));
        List<String> roles = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(userRoles)) {
            roles = userRoles.stream().map(UserRole::getRoleId).collect(toList());
        }
        response.setRole(roles);


        response.setName(user.getName());
        response.setUserId(user.getUserId());
        response.setLoginName(user.getLoginName());
        response.setMobile(user.getMobile());
        response.setEmail(user.getEmail());
        response.setProductLineId(user.getProductLineId());
        response.setProductLineName(user.getProductLineName());
        response.setClientId(user.getClientId());
        String anonymousIp = IpUtil.getRemoteAddress(request);
        if (Objects.equals(Constants.ANONYMOUS_IP, anonymousIp)) {
            anonymousIp = "127.0.0.1";
        }
        if (StringUtils.isBlank(anonymousIp)) {
            anonymousIp = "127.0.0.1";
        }
        response.setToken(UUID.randomUUID().toString().replaceAll("-", ""));
        String key = Constants.REDIS_KEY_USERID + user.getUserId();
        if (redisService.hasKey(key)) {
            redisService.del(redisService.get(key).toString());
            Serializable token = redisService.get(key);
            if (token != null && StringUtils.isNotBlank(token.toString())) {
                redisService.del(token.toString());
            }
        }
        redisService.del(key);

        GlobalUser globalUser = new GlobalUser();
        globalUser.setDeptId(response.getDeptId());
        globalUser.setRoleIds(roles);
        globalUser.setLoginName(response.getLoginName());
        globalUser.setIpAddress(anonymousIp);
        globalUser.setUserId(response.getUserId());
        globalUser.setName(user.getName());
        globalUser.setProductLineId(user.getProductLineId());
        globalUser.setProductLineName(user.getProductLineName());
        if (NINE_STR.equals(user.getUserType())) {
            globalUser.setUserType(NINE_STR);
        } else {
            globalUser.setUserType(params.getUserType());
        }
        String json = JSON.toJSONString(globalUser);
        if (NINE_STR.equals(user.getUserType())) {
            redisService.set(response.getToken(), json);
            redisService.set(key, response.getToken());
        } else {
            if (Constants.ONE_STR.equals(params.getUserType())) {
                redisService.set(response.getToken(), json, Constants.PC_TOKEN_CODE_EXPIRE);
                redisService.set(key, response.getToken(), Constants.REDIS_USERID_EXPIRE);
            } else if (Constants.TWO_STR.equals(params.getUserType())) {
                log.info("APP登录 设置过期时长30天");
                redisService.set(response.getToken(), json, Constants.APP_TOKEN_CODE_EXPIRE);
                redisService.set(key, response.getToken(), Constants.APP_TOKEN_CODE_EXPIRE);
                //用于用户登录前ClientId验证
                redisService.set(Constants.CLIENT_ID + response.getToken(), JSON.toJSONString(response), Constants.APP_TOKEN_CODE_EXPIRE);
            } else {
                redisService.set(response.getToken(), json, Constants.PAD_TOKEN_CODE_EXPIRE);
                redisService.set(key, response.getToken(), Constants.REDIS_USERID_EXPIRE);
            }

        }

        //更新用户客户端ID
        user.setClientId(params.getClientId());
        baseMapper.updateById(user);
        return ResultBody.success(response);
    }


    /**
     * 通过客户端ID验证用户是否需要登录
     *
     * @param clientId
     * @return
     */
    public ResultBody checkUserByClientId(String clientId) {
        //通过clientId获取用户ID集合
        List<String> userIds = baseMapper.getUserByClientId(clientId);

        if (CollectionUtils.isEmpty(userIds)) {
            return ResultBody.fail(UserErrorCode.USER_LOGIN_EXPIRY.getCode(), UserErrorCode.USER_LOGIN_EXPIRY.getMsg());
        } else {
            for (String userId : userIds) {
                String key = Constants.REDIS_KEY_USERID + userId;
                Serializable token = redisService.get(key);
                if (token != null && StringUtils.isNotBlank(token.toString())) {
                    Serializable response = redisService.get(Constants.CLIENT_ID + token.toString());
                    if (response != null && StringUtils.isNotBlank(response.toString())) {
                        return ResultBody.success(JSON.parseObject(response.toString(), BindResponse.class));
                    }

                }

            }
        }
        return ResultBody.fail(UserErrorCode.USER_LOGIN_EXPIRY.getCode(), UserErrorCode.USER_LOGIN_EXPIRY.getMsg());
    }


    /**
     * 登录
     *
     * @param params
     * @return
     */
    public ResultBody login(LoginParams params, HttpServletRequest request) {
        if (!Optional.ofNullable(params).isPresent()) {
            return ResultBody.fail(UserErrorCode.PARAM_ERROR.getCode(), UserErrorCode.PARAM_ERROR.getMsg());
        }
        String userName = params.getUsername();
        if (StringUtils.isBlank(userName)) {
            return ResultBody.fail(UserErrorCode.USERNAME_IS_NULL.getCode(), UserErrorCode.USERNAME_IS_NULL.getMsg());
        }
        String password = params.getPassword();
        if (StringUtils.isBlank(password)) {
            return ResultBody.fail(UserErrorCode.PASSWORD_IS_NULL.getCode(), UserErrorCode.PASSWORD_IS_NULL.getMsg());
        }

        //解析密码
        try {
            password = RSAEncrypt.decrypt(password, Constants.PASSWORD_PRIVATE_KEY);
        } catch (Exception e) {
            return ResultBody.fail(UserErrorCode.PASSWORD_IS_ERROR.getCode(), UserErrorCode.PASSWORD_IS_ERROR.getMsg());
        }

        /*String verifyCode = params.getVerifyCode();
        if (StringUtils.isBlank(verifyCode)) {
            return ResultBody.fail(UserErrorCode.GRAPHIC_IS_NULL.getCode(), UserErrorCode.GRAPHIC_IS_NULL.getMsg());
        }*/

        String userType = params.getUserType();
        if (StringUtils.isBlank(userType)) {
            return ResultBody.fail(UserErrorCode.USERTYPE_IS_NULL.getCode(), UserErrorCode.USERTYPE_IS_NULL.getMsg());
        }
        //图形验证码是否存在
        /*if (!redisService.hasKey(Constants.GRAPH_CODE_KEY.concat(verifyCode))) {
            return ResultBody.fail(UserErrorCode.GRAPHIC_IS_ERROR.getCode(), UserErrorCode.GRAPHIC_IS_ERROR.getMsg());
        }*/
        //根据用户名查询用户是否存在
        UserEntity user = baseMapper.getUserByLoginName(userName);

        if (!Optional.ofNullable(user).isPresent()) {
            return ResultBody.fail(UserErrorCode.USER_IS_NOT_EXISTS.getCode(), UserErrorCode.USER_IS_NOT_EXISTS.getMsg());
        }
        //是否被禁用
        if (!user.normal()) {
            return ResultBody.fail(UserErrorCode.USER_DISABLE.getCode(), UserErrorCode.USER_DISABLE.getMsg());
        }
        BindResponse response = new BindResponse();

        //密码是否正确
        if (!user.getPassword().equals(Encrypt.encryPwd(user.getSalt(), password))) {
            return ResultBody.fail(UserErrorCode.PASSWORD_IS_ERROR.getCode(), UserErrorCode.PASSWORD_IS_ERROR.getMsg());
        }

        if (StringUtils.isBlank(response.getDeptId())) response.setDeptId(user.getDeptId());
        DeptEntity deptEntity = deptService.getDept(user.getDeptId());
        if (deptEntity != null) {
            response.setDeptName(deptEntity.getDeptName());
            response.setDeptType(deptEntity.getDeptType());
        }
//        List<String> roles = userRoleMapper.getRoleIdList(user.getUserId());
        List<UserRole> userRoles = userRoleMapper.getRoleByUserIds(Collections.singletonList(user.getUserId()));
        List<String> roles = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(userRoles)) {
            roles = userRoles.stream().map(UserRole::getRoleId).collect(toList());
        }
        response.setRole(roles);

        response.setName(user.getName());
        response.setUserId(user.getUserId());
        response.setLoginName(user.getLoginName());
        response.setMobile(user.getMobile());
        response.setEmail(user.getEmail());
        response.setProductLineId(user.getProductLineId());
        response.setProductLineName(user.getProductLineName());
        response.setClientId(user.getClientId());
        String anonymousIp = IpUtil.getRemoteAddress(request);
        if (Objects.equals(Constants.ANONYMOUS_IP, anonymousIp)) {
            anonymousIp = "127.0.0.1";
        }
        if (StringUtils.isBlank(anonymousIp)) {
            anonymousIp = "127.0.0.1";
        }
        response.setToken(UUID.randomUUID().toString().replaceAll("-", ""));
        String key = "";
        if (Constants.TWO_STR.equals(userType)) {
            key = Constants.REDIS_KEY_APP_USERID + user.getUserId();
        } else {
            key = Constants.REDIS_KEY_USERID + user.getUserId();
        }

        if (redisService.hasKey(key)) {
            Serializable token = redisService.get(key);
            redisService.del(key);
            if (token != null && StringUtils.isNotBlank(token.toString())) {
                redisService.del(token.toString());
            }
        }

        //登录成功,删除图片验证码
        //redisService.del(Constants.GRAPH_CODE_KEY.concat(verifyCode));
        GlobalUser globalUser = new GlobalUser();
        globalUser.setDeptId(response.getDeptId());
        globalUser.setRoleIds(roles);
        globalUser.setLoginName(response.getLoginName());
        globalUser.setProductLineId(user.getProductLineId());
        globalUser.setProductLineName(user.getProductLineName());
        globalUser.setIpAddress(anonymousIp);
        globalUser.setUserId(response.getUserId());
        globalUser.setName(user.getName());
        if (NINE_STR.equals(user.getUserType())) {
            globalUser.setUserType(NINE_STR);
        } else {
            globalUser.setUserType(userType);
        }
        String json = JSON.toJSONString(globalUser);
        if (NINE_STR.equals(user.getUserType())) {
            redisService.set(response.getToken(), json);
            redisService.set(key, response.getToken());
        } else {
            if (Constants.ONE_STR.equals(userType)) {
                redisService.set(response.getToken(), json, Constants.PC_TOKEN_CODE_EXPIRE);
                redisService.set(key, response.getToken(), Constants.REDIS_USERID_EXPIRE);
            } else {
                redisService.set(response.getToken(), json, Constants.APP_TOKEN_CODE_EXPIRE);
                redisService.set(key, response.getToken(), Constants.APP_TOKEN_CODE_EXPIRE);
            }
        }
        return ResultBody.success(response);
    }

    /***
     * 根据LoginName获取登录信息
     * @param loginName
     * @param request
     * @return
     */
    public ResultBody getLoginByLoginName(String loginName, HttpServletRequest request) {
        //根据用户名查询用户是否存在
        UserEntity user = baseMapper.getUserByLoginName(loginName);

        if (!Optional.ofNullable(user).isPresent()) {
            return ResultBody.fail(UserErrorCode.USER_IS_NOT_EXISTS.getCode(), UserErrorCode.USER_IS_NOT_EXISTS.getMsg());
        }

        BindResponse response = new BindResponse();

        List<UserRole> userRoles = userRoleMapper.getRoleByUserIds(Collections.singletonList(user.getUserId()));
        List<String> roles = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(userRoles)) {
            roles = userRoles.stream().map(UserRole::getRoleId).collect(toList());
        }
        response.setRole(roles);

        response.setName(user.getName());
        response.setUserId(user.getUserId());
        response.setLoginName(user.getLoginName());
        response.setMobile(user.getMobile());
        response.setEmail(user.getEmail());
        response.setProductLineId(user.getProductLineId());
        response.setProductLineName(user.getProductLineName());
        response.setClientId(user.getClientId());

        DeptEntity deptEntity = deptService.getDept(user.getDeptId());
        if (deptEntity != null) {
            response.setDeptName(deptEntity.getDeptName());
            response.setDeptType(deptEntity.getDeptType());
        }

        String key = Constants.REDIS_KEY_USERID + user.getUserId();
        Serializable token = redisService.get(key);
        if (token != null) {
            Serializable json = redisService.get(token.toString());
            if (json != null && StringUtils.isNotBlank(json.toString())) {
                GlobalUser globalUser = JSON.parseObject(json.toString(), GlobalUser.class);
                if (globalUser != null) {
                    response.setToken(token.toString());
                    return ResultBody.success(response);
                }
            }
        }

        response.setToken(UUID.randomUUID().toString().replaceAll("-", ""));

        GlobalUser globalUser = new GlobalUser();
        globalUser.setDeptId(response.getDeptId());
        globalUser.setRoleIds(roles);
        globalUser.setLoginName(response.getLoginName());
        globalUser.setProductLineId(user.getProductLineId());
        globalUser.setProductLineName(user.getProductLineName());
        String anonymousIp = IpUtil.getRemoteAddress(request);
        if (Objects.equals(Constants.ANONYMOUS_IP, anonymousIp)) {
            anonymousIp = "127.0.0.1";
        }
        if (StringUtils.isBlank(anonymousIp)) {
            anonymousIp = "127.0.0.1";
        }
        globalUser.setIpAddress(anonymousIp);
        globalUser.setUserId(response.getUserId());
        globalUser.setName(user.getName());
        globalUser.setUserType(user.getUserType());
        String json = JSON.toJSONString(globalUser);
        if (NINE_STR.equals(user.getUserType())) {
            redisService.set(response.getToken(), json);
            redisService.set(key, response.getToken());
        } else {
            if (Constants.ONE_STR.equals(user.getUserType())) {
                redisService.set(response.getToken(), json, Constants.PC_TOKEN_CODE_EXPIRE);
                redisService.set(key, response.getToken(), Constants.REDIS_USERID_EXPIRE);
            } else {
                redisService.set(response.getToken(), json, Constants.APP_TOKEN_CODE_EXPIRE);
                redisService.set(key, response.getToken(), Constants.APP_TOKEN_CODE_EXPIRE);
            }
        }

        return ResultBody.success(response);
    }


    /**
     * 短信登录接口
     *
     * @param noteLoginReq
     * @return
     */
    public ResultBody<BindResponse> noteLogin(NoteLoginReq noteLoginReq, HttpServletRequest request) {
        log.info("登录手机号:" + noteLoginReq.getPhoneNum());
        if (!redisService.hasKey(Constants.NOTE_VERIFICATION_CODE.concat(noteLoginReq.getPhoneNum()))) {
            return ResultBody.fail(UserErrorCode.NOTE_VCODE_OUT_OF_WORK.getCode(), UserErrorCode.NOTE_VCODE_OUT_OF_WORK.getMsg());
        }
        String vCode = redisService.get(Constants.NOTE_VERIFICATION_CODE.concat(noteLoginReq.getPhoneNum())).toString();
        if (!noteLoginReq.getVCode().equals(vCode)) {
            return ResultBody.fail(UserErrorCode.NOTE_VCODE_OUT_OF_WORK.getCode(), UserErrorCode.NOTE_VCODE_OUT_OF_WORK.getMsg());
        }

        //登录模式，校验用户有效性
        UserEntity user = baseMapper.getUserByPhone(noteLoginReq.getPhoneNum());
        if (user == null) {
            return ResultBody.fail(UserErrorCode.PHONENUM_IS_NOT_REGISTER.getCode(), UserErrorCode.PHONENUM_IS_NOT_REGISTER.getMsg());
        }

        BindResponse response = new BindResponse();

        if (StringUtils.isBlank(response.getDeptId())) response.setDeptId(user.getDeptId());
        DeptEntity deptEntity = deptService.getDept(user.getDeptId());
        if (deptEntity != null) {
            response.setDeptName(deptEntity.getDeptName());
            response.setDeptType(deptEntity.getDeptType());
        }
        List<UserRole> userRoles = userRoleMapper.getRoleByUserIds(Collections.singletonList(user.getUserId()));
        List<String> roles = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(userRoles)) {
            roles = userRoles.stream().map(UserRole::getRoleId).collect(toList());
        }

        response.setRole(roles);
        response.setName(user.getName());
        response.setUserId(user.getUserId());
        response.setLoginName(user.getLoginName());
        response.setMobile(user.getMobile());
        response.setEmail(user.getEmail());
        response.setProductLineId(user.getProductLineId());
        response.setClientId(user.getClientId());
        String anonymousIp = IpUtil.getRemoteAddress(request);
        if (Objects.equals(Constants.ANONYMOUS_IP, anonymousIp)) {
            anonymousIp = "127.0.0.1";
        }
        if (StringUtils.isBlank(anonymousIp)) {
            anonymousIp = "127.0.0.1";
        }
        response.setToken(UUID.randomUUID().toString().replaceAll("-", ""));
        String key = Constants.REDIS_KEY_USERID + user.getUserId();
        if (redisService.hasKey(key)) {
            redisService.del(redisService.get(key).toString());
        }
        redisService.del(key);

        //登录成功,删除短信验证码
        redisService.del(Constants.NOTE_VERIFICATION_CODE.concat(noteLoginReq.getPhoneNum()));

        GlobalUser globalUser = new GlobalUser();
        globalUser.setDeptId(response.getDeptId());
        globalUser.setRoleIds(roles);
        globalUser.setLoginName(response.getLoginName());
        globalUser.setName(user.getName());
        globalUser.setIpAddress(anonymousIp);
        globalUser.setUserId(response.getUserId());
        globalUser.setRoleIds(userRoles.stream().map(UserRole::getRoleId).collect(toList()));
        String json = JSON.toJSONString(globalUser);

        redisService.set(response.getToken(), json, Constants.PC_TOKEN_CODE_EXPIRE);
        redisService.set(key, response.getToken(), Constants.REDIS_USERID_EXPIRE);

        return ResultBody.success(response);
    }

    /**
     * 获取短信验证码
     *
     * @param phoneNum
     * @return
     */
    public ResultBody getNoteCode(String phoneNum) {
/*        if(redisService.hasKey(Constants.NOTE_VERIFICATION_CODE.concat(phoneNum))){
            return ResultBody.fail(UserErrorCode.USER_CURRENT_ONLINE.getCode(),UserErrorCode.USER_CURRENT_ONLINE.getMsg());
        }*/
        String captcha = RandomUtil.randomNumbers(6);
        if (sendSms(phoneNum, captcha)) {
            redisService.set(Constants.NOTE_VERIFICATION_CODE.concat(phoneNum), captcha, Constants.NOTE_VERIFICATION_EXPIRE);
            return ResultBody.success();
        } else {
            return ResultBody.fail(UserErrorCode.SEND_NOTE_VCODE_ERROR.getCode(), UserErrorCode.SEND_NOTE_VCODE_ERROR.getMsg());
        }
    }


    /**
     * 退出登录
     *
     * @param token
     * @return
     */
    public ResultBody<Void> logout(String token) {
        if (StringUtils.isBlank(token)) {
            return ResultBody.success();
        }
//        ssoToken.deleteToken(token);
        redisService.del(token);
        redisService.del(Constants.CLIENT_ID + token);
        return ResultBody.success();
    }

    /***
     * 获取部门名称
     * @param deptTree
     * @param deptId
     * @param deptName
     */
    private void getDeptName(Tree<DeptEntity> deptTree, String deptId, StringBuilder deptName) {
        deptName.append(deptTree.getText()).append("/");
        if (!deptId.equals(deptTree.getId())) {
            if (CollectionUtils.isNotEmpty(deptTree.getChildren())) {
                for (Tree<DeptEntity> tree : deptTree.getChildren()) {
                    if (deptId.equals(tree.getId())) {
                        deptName.append(tree.getText()).append("/");
                    } else {
                        getDeptName(tree, deptId, deptName);
                    }
                }
            }
        }
    }

    /***
     * 获取所有用户Id,并保存到redis中
     * @return
     */
    public List<String> getUserIds() {
        List<String> userIds = this.list().stream().map(u -> u.getUserId()).collect(toList());
        if (CollectionUtils.isNotEmpty(userIds)) {
            redisService.del(Constants.CHECK_LOGIN_PARAM_INFO);
            redisService.set(Constants.CHECK_LOGIN_PARAM_INFO, JSON.toJSONString(userIds));
        }
        return userIds;
    }


    /**
     * 获取userId
     *
     * @return
     */
    public String getUserId() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Object token = request.getAttribute("token");
        if (token == null) {
            token = request.getHeader("token");
        }
        if (Optional.ofNullable(token).isPresent()) {
            if (redisService.hasKey(token.toString())) {
                return redisService.get(token.toString()).toString().split(",")[1];
            }
        }
        return null;
    }

    /***
     * 设置客户端Id
     * @param cid
     */
    public void setClientId(String cid) {
        String userId = getUserId();
        if (StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(cid)) {
            baseMapper.setClientId(userId, cid);
        }
    }

    /**
     * 发送短信接口
     *
     * @param phone
     * @return
     */
    public boolean sendSms(String phone, String vcode) {
/*        try {
            SendSmsResponse smsResponse = smsUtil.sendSms(phone,"{code:"+vcode+"}");
            if(smsResponse!=null){
                if("OK".equals(smsResponse.getCode())){
                    return true;
                }
            }
        } catch (ClientException e) {
            log.info("发送短信验证码错误:"+e.getErrMsg());
            return false;
        }*/
        return false;
    }

    /**
     * 校验手机号是否存在
     *
     * @param phoneNum
     * @return
     */
    public ResultBody checkPhoneNum(String phoneNum) {
        //校验手机号用户是否存在
        UserEntity user = baseMapper.getUserByPhone(phoneNum);
        if (user == null) {
            return ResultBody.fail(UserErrorCode.PHONENUM_IS_NOT_REGISTER.getCode(), UserErrorCode.PHONENUM_IS_NOT_REGISTER.getMsg());
        }

        return ResultBody.success();
    }

    public List<DictInfo> getUserNamesByUserIds(List<String> userIds) {
        return baseMapper.getUserNamesByUserIds(userIds);
    }


    /***
     * 获取用户手机号
     * @param userIds
     * @return
     */
    public List<String> getUserPhones(List<String> userIds) {
        return baseMapper.getUserPhones(userIds);
    }

    /**
     * 检查用户Id是否存在
     *
     * @param userId
     * @return
     */
    public boolean checkUser(String userId) {
        String existUserId = baseMapper.checkUser(userId);
        if (StringUtils.isNotBlank(existUserId)) {
            List<String> userIds = this.list().stream().map(UserEntity::getUserId).collect(toList());
            if (CollectionUtils.isNotEmpty(userIds)) {
                redisService.del(Constants.CHECK_LOGIN_PARAM_INFO);
                redisService.set(Constants.CHECK_LOGIN_PARAM_INFO, JSON.toJSONString(userIds));
            }
            return true;
        }
        return false;
    }


    public static void main(String[] args) throws Exception {
        String decrypt = RSAEncrypt.decrypt("lTl7rD5fD3L7qI7TbTmV1wLPR1SeqU9dHPdQVdjfnis7PYCTFP/LmAVmuhov8v7EvPZYxvDopaE7UvAeXGEMvba5UHpuieRrcbTZtrRbtI6V8TcI1f/hdADiM7igozupDMDoLAW40ZqYX95xFXghtzl7KS3POxRRIBkrdYlVM9k=", Constants.PASSWORD_PRIVATE_KEY);
        String salt = Encrypt.getSalt();
        System.out.println(salt);
        String pwd = Encrypt.encryPwd(salt, decrypt);
        System.out.println(pwd);
    }


    /**
     * 获取用户所有角色
     *
     * @param userId
     * @return
     */
    public ResultBody<List<String>> getUserRoles(String userId) {
        return ResultBody.success((userRoleMapper.getRoleByUserId(userId)).stream().map(UserRole::getRoleName).collect(Collectors.toList()));
    }

    /**
     * @Description: 根据用户ids查询用户信息
     * @version v1.0
     * @author songJinKang
     * @date 2023-11-13 20:20
     */
    public List<UserEntity> getUserByUserIds(List<String> userIds) {
        if (CollectionUtils.isNotEmpty(userIds)) {
            return lambdaQuery().in(UserEntity::getUserId, userIds).list();
        }
        return Collections.emptyList();
    }

    /***
     * 根据部门ID列表获取用户信息列表
     * @param deptIds
     * @return
     */
    public List<UserEntity> getUserByDeptIds(List<String> deptIds) {
        return baseMapper.getUserByDeptIds(deptIds);
    }

    /***
     * 根据部门ID获取用户信息列表
     * @param deptId
     * @return
     */
    public List<UserEntity> getUserByDeptId(String deptId) {
        return baseMapper.getUserByDeptId(deptId);
    }

}
